package com.aishua.fire.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.aishua.fire.common.JsonArrayResult;
import com.aishua.fire.common.JsonObjectResult;
import com.aishua.fire.common.ValidateUtil;
import com.aishua.fire.common.anno.ModuleService;
import com.aishua.fire.common.constant.BigMenuConstants;
import com.aishua.fire.common.constant.ModuleConstants;
import com.aishua.fire.common.exp.ValidateException;
import com.aishua.fire.entity.drillrecord.DrillRecordCatalogInfo;
import com.aishua.fire.entity.drillrecord.DrillRecordExampleInfo;
import com.aishua.fire.entity.drillrecord.DrillRecordTitleInfo;
import com.aishua.fire.service.DrillRecordService;
import com.github.pagehelper.PageInfo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

@Api(value = "演练计划管理-演练现场记录",tags = {"演练计划管理-演练现场记录"})
@RestController
@Slf4j
public class DrillRecordManagerController
{

	@Autowired
	private DrillRecordService drillRecordService;
	
	@Autowired
	private ValidateUtil validateUtil;
	
	@SuppressWarnings("rawtypes")
	@ModuleService(totalModule=BigMenuConstants.checkManager,moduleName=ModuleConstants.checkItem)
	@ApiOperation(value="演练计划管理-演练现场记录-新增演练记录题目")
	@PostMapping(value = {"/drill/title"} )
	public ResponseEntity<JsonObjectResult> add(@RequestBody DrillRecordTitleInfo info, BindingResult result) throws ValidateException
	{
		log.info("演练计划管理-演练现场记录-新增演练记录题目:{}",info);
		validateUtil.respErrorMsg(result);//非空验证
		
		boolean flag = drillRecordService.save(info);
		
		JsonObjectResult r = null;
		if(flag)
		{
			r = JsonObjectResult.makeSuccessResult(null);
		}
		else
		{
			r = JsonObjectResult.makeErrorResult("检查项目名称重复");
		}
		return ResponseEntity.ok(r);
    }
	
	@SuppressWarnings("rawtypes")
    @ModuleService(totalModule=BigMenuConstants.checkManager,moduleName=ModuleConstants.checkItem)
    @ApiOperation(value="演练计划管理-演练现场记录-新增演练记录题目二级目录")
    @PostMapping(value = {"/drill/catalog"} )
    public ResponseEntity<JsonObjectResult> addCatalog(@RequestBody DrillRecordCatalogInfo info, BindingResult result) throws ValidateException
    {
        log.info("演练计划管理-演练现场记录-新增演练记录二级题目:{}",info);
        validateUtil.respErrorMsg(result);//非空验证
        
        boolean flag = drillRecordService.addCatalog(info);
        
        JsonObjectResult r = null;
        if(flag)
        {
            r = JsonObjectResult.makeSuccessResult(null);
        }
        else
        {
            r = JsonObjectResult.makeErrorResult("检查项目名称重复");
        }
        return ResponseEntity.ok(r);
    }
	
	@ApiOperation(value = "分页查询演练计划管理-演练现场记录-演练记录题目")
    @ModuleService(totalModule = BigMenuConstants.emergencyPre, moduleName = ModuleConstants.ENERGENCYPRE)
    @GetMapping(value = { "/drill/title" })
    public ResponseEntity<JsonArrayResult<DrillRecordTitleInfo>> drillTitlePreList(
            @RequestParam(name = "pageNumber") int pageNumber, @RequestParam(name = "pageSize") int pageSize,
            @RequestParam(name = "title_name") String title_name, @RequestParam(name = "title_type") String title_type) {
        JsonArrayResult<DrillRecordTitleInfo> r = null;
        List<DrillRecordTitleInfo> searchList = drillRecordService.drillTitlePreList(pageNumber, pageSize, title_name,
                title_type);
        if (searchList.size() > 0) {
            PageInfo<DrillRecordTitleInfo> pages = new PageInfo<DrillRecordTitleInfo>(searchList);
            r = JsonArrayResult.makeSuccessResult(pages.getList(), (int) pages.getTotal());
            return ResponseEntity.ok(r);
        } else {
            r = JsonArrayResult.makeSuccessResult(searchList, 0);
            return ResponseEntity.ok(r);
        }
    }
	
	@ApiOperation(value = "分页查询演练计划管理-演练现场记录-演练记录题目二级目录")
    @ModuleService(totalModule = BigMenuConstants.emergencyPre, moduleName = ModuleConstants.ENERGENCYPRE)
    @GetMapping(value = { "/drill/catalog" })
    public ResponseEntity<JsonArrayResult<DrillRecordCatalogInfo>> catalogList(@RequestParam(name = "title_id") String title_id) {
        JsonArrayResult<DrillRecordCatalogInfo> r = null;
        List<DrillRecordCatalogInfo> searchList = drillRecordService.catalogList(title_id);
            r = JsonArrayResult.makeSuccessResult(searchList, 0);
            return ResponseEntity.ok(r);
    }
	
    @ApiOperation(value = "分页查询演练计划管理-演练现场记录-演练记录习题列表")
    @ModuleService(totalModule = BigMenuConstants.emergencyPre, moduleName = ModuleConstants.ENERGENCYPRE)
    @GetMapping(value = { "/drill/example" })
    public ResponseEntity<JsonArrayResult<DrillRecordExampleInfo>> exampleList(@RequestParam(name = "catalog_id") String catalog_id) {
        JsonArrayResult<DrillRecordExampleInfo> r = null;
        List<DrillRecordExampleInfo> searchList = drillRecordService.exampleList(catalog_id);
            r = JsonArrayResult.makeSuccessResult(searchList, 0);
            return ResponseEntity.ok(r);
    }
	
    
    
    @SuppressWarnings("rawtypes")
    @ModuleService(totalModule=BigMenuConstants.checkManager,moduleName=ModuleConstants.checkItem)
    @ApiOperation(value="演练计划管理-演练现场记录-新增演练记录习题")
    @PostMapping(value = {"/drill/example"} )
    public ResponseEntity<JsonObjectResult> addexample(@RequestBody DrillRecordExampleInfo info, BindingResult result) throws ValidateException
    {
        log.info("演练计划管理-演练现场记录-新增演练记录习题:{}",info);
        validateUtil.respErrorMsg(result);//非空验证
        
        boolean flag = drillRecordService.addexample(info);
        
        JsonObjectResult r = null;
        if(flag)
        {
            r = JsonObjectResult.makeSuccessResult(null);
        }
        else
        {
            r = JsonObjectResult.makeErrorResult("检查项目名称重复");
        }
        return ResponseEntity.ok(r);
    }
	
	/*
	@SuppressWarnings("rawtypes")
	@ModuleService(totalModule=BigMenuConstants.checkManager,moduleName=ModuleConstants.checkItem)
	@ApiOperation(value="巡检管理-检查项目新增检查明细")
	@PostMapping(value = {"/checkitem/detail"} )
	public ResponseEntity<JsonObjectResult> addCheckitemDetail(@RequestBody AddCheckItemDetailInfo info, BindingResult result) throws ValidateException
	{
		log.info("巡检管理-检查项目新增检查明细:{}",info);
		validateUtil.respErrorMsg(result);//非空验证
		
		drillRecordService.addCheckitemDetail(info);
		boolean flag = 
		
		JsonObjectResult r = null;
		if(flag)
		{
			r = JsonObjectResult.makeSuccessResult(null);
		}
		else
		{
			r = JsonObjectResult.makeErrorResult("名称重复");
		}
		JsonObjectResult r = JsonObjectResult.makeSuccessResult(null);
		return ResponseEntity.ok(r);
    }
	
	
	*//**
	 * 巡检管理-查询检查项目列表
	 *//*
	@SuppressWarnings("rawtypes")
	@ApiOperation(value = "巡检管理-查询检查项目列表")
	@ModuleService(totalModule=BigMenuConstants.checkManager,moduleName=ModuleConstants.checkItem)
	@GetMapping(value = { "/checkitem" })
	public ResponseEntity<JsonArrayResult> list(
			@RequestParam(name = "item_name") String item_name,@RequestParam(name = "item_num") String item_num,
			@RequestParam(name = "item_type") String item_type,
			@RequestParam(name = "pageNumber") int pageNumber, @RequestParam(name = "pageSize") int pageSize) 
	{
		log.info("巡检管理-查询检查项目列表  查询列表请求参数:{}", item_name);
		
		List<CheckItemInfo> catalogs = drillRecordService.list(item_name,item_num, item_type,pageNumber, pageSize);
		int total = drillRecordService.listTotal(item_name,item_num,item_type);
		log.info("查询总条数：{}", total);
		JsonArrayResult r = JsonArrayResult.makeSuccessResult(catalogs, total);

		return ResponseEntity.ok(r);
	}
	
	*//**
	 * 巡检管理-查询检查项目详情
	 *//*
	@SuppressWarnings("rawtypes")
	@ApiOperation(value = "巡检管理-查询检查项目详情")
	@ModuleService(totalModule=BigMenuConstants.checkManager,moduleName=ModuleConstants.checkItem)
	@GetMapping(value = { "/checkitem/detail" })
	public ResponseEntity<JsonObjectResult> iteminfo(@RequestParam(name = "id") String id) 
	{
		log.info("巡检管理-查询检查项目详情  查询列表请求参数:{}", id);
		
		CheckItemInfo iteminfo = drillRecordService.getItemInfo(id);
		log.info("查询详情：{}", iteminfo);
		JsonObjectResult r = JsonObjectResult.makeSuccessResult(iteminfo);

		return ResponseEntity.ok(r);
	}
	
	@SuppressWarnings("rawtypes")
	@ModuleService(totalModule=BigMenuConstants.checkManager,moduleName=ModuleConstants.checkManageritem)
	@ApiOperation(value="巡检管理-修改检查项目")
	@PutMapping(value = {"/checkitem"} )
	public ResponseEntity<JsonObjectResult> update(@RequestBody CheckItemInfo info, BindingResult result) throws ValidateException
	{
		log.info("检查项目分类目录_修改检查项目信息:{}",info);
		validateUtil.respErrorMsg(result);//非空验证
		drillRecordService.update(info);
		JsonObjectResult r = JsonObjectResult.makeSuccessResult(null);

		return ResponseEntity.ok(r);
    }
	
	*//**
     * 巡检管理-查询检查项目列表
     *//*
    @SuppressWarnings("rawtypes")
    @ApiOperation(value = "巡检管理-巡检点管理-查询当前巡检点关联的检查项目列表")
    @ModuleService(totalModule=BigMenuConstants.checkManager,moduleName=ModuleConstants.checkItem)
    @GetMapping(value = { "/point/checkitem" })
    public ResponseEntity<JsonArrayResult> checkitemlist(
            @RequestParam(name = "p_id") String p_id) 
    {
        log.info("巡检管理-查询检查项目列表  查询列表请求参数:{}", p_id);
        
        List<CheckItemInfo> catalogs = drillRecordService.checkitemlist(p_id);
        log.info("查询总条数：{}", catalogs);
        JsonArrayResult r = JsonArrayResult.makeSuccessResult(catalogs, 0);

        return ResponseEntity.ok(r);
    }*/
	
}